package com.shijie.newvideo.pagerslidingtabstrip;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;

public class DensityUtil {
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 取设备的屏幕密度并输出对应的资源文件夹名称：
     */
    public static String getDensityFolderName(Context context) {
        Resources res = context.getResources();
        DisplayMetrics metrics = res.getDisplayMetrics();
        int densityDpi = metrics.densityDpi;
        String folderName = "values"; // 默认为 "values"
        if (densityDpi <= DisplayMetrics.DENSITY_HIGH) {
            folderName = "ldpi";
        } else if (densityDpi <= DisplayMetrics.DENSITY_XHIGH) {
            folderName = "mdpi";
        } else if (densityDpi <= DisplayMetrics.DENSITY_XXHIGH) {
            folderName = "xhdpi";
        } else if (densityDpi <= DisplayMetrics.DENSITY_XXXHIGH) {
            folderName = "xxhdpi";
        } else {
            folderName = "xxxhdpi"; // 更高密度的设备，可以根据需要添加更多判断条件
        }
        return folderName;
    }
}
